/**
 * Created by gongxun on 16/12/19.
 */

var KwxDialogSetting = GameCCSBase.extend({
    Slider_effect : null,
    Slider_music : null,
    Image_localismSelect : null,
    Text_guangshui : null,
    Text_putong : null,
    mCardBgUIs : null,
    Image_tabs : null,
    Panel_setting : null,
    Panel_huamian : null,

    //方言控件
    mPanel_yuyans : null,

    //游戏名称
    mGameName : null,
    ctor: function (gameName) {
        this._super();
        this.mGameName = gameName == undefined ? "kwx" : gameName;
        this.loadCocostudio("res/ccs/KwxSettingLayer.json");
        this.Image_localismSelect = mo.seekWidgetByName(this.ccsNode, "Image_localismSelect");
        this.Text_guangshui = mo.seekWidgetByName(this.ccsNode, "Text_guangshui");
        this.Text_putong = mo.seekWidgetByName(this.ccsNode, "Text_putong");
        this.Panel_setting = mo.seekWidgetByName(this.ccsNode, "Panel_setting");
        this.Panel_huamian = mo.seekWidgetByName(this.ccsNode, "Panel_huamian");

        //button
        mo.seekWidgetByName(this.ccsNode, "Button_close").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.setDialog(false);
            }
        }, this);
        mo.seekWidgetByName(this.ccsNode, "Panel_touch").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.setDialog(false);
            }
        }, this);

        //退出
        mo.seekWidgetByName(this.ccsNode, "Button_ok").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.setDialog(false);
                this.sendJiesanTable();
            }
        }, this);

        this.Slider_effect = mo.seekWidgetByName(this.ccsNode, "Slider_effect");
        this.Slider_music = mo.seekWidgetByName(this.ccsNode, "Slider_music");
        this.Slider_effect.addEventListener(this.sliderEvent, this);
        this.Slider_music.addEventListener(this.sliderEvent, this);

        //effects
        var Panel_effect = mo.seekWidgetByName(this.ccsNode, "Panel_effect");
        mo.seekWidgetByName(Panel_effect, "Image_close").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameUserInfo.setEffectVoice(0);
            }
        }, this);

        mo.seekWidgetByName(Panel_effect, "Image_open").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameUserInfo.setEffectVoice(100);
            }
        }, this);

        //music
        var Panel_music = mo.seekWidgetByName(this.ccsNode, "Panel_music");
        mo.seekWidgetByName(Panel_music, "Image_close").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameUserInfo.setMusicVoice(0);
            }
        }, this);

        mo.seekWidgetByName(Panel_music, "Image_open").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameUserInfo.setMusicVoice(100);
            }
        }, this);

        //方言
        this.mPanel_yuyans = [];
        var Panel_yuyan = mo.seekWidgetByName(this.ccsNode, "Panel_yuyan");
        for (var i = 0; i < 6; i++){
            var item = mo.seekWidgetByName(Panel_yuyan, "Panel_yuyan" + i);
            item.setTag(i);
            this.mPanel_yuyans.push(item);

            item.addTouchEventListener(function (sender, type) {
                if (mo.gameHelper.getIsTouchEnd(type)) {
                    var index = sender.getTag();
                    mo.gameUserInfo.setLocalism(index);
                }
            }, this);
        }

        //选择ui
        this.Image_tableBgSelect = mo.seekWidgetByName(this.ccsNode, "Image_tableBgSelect");
        for (var i = 0; i < 6; i++){
            var Image_tableBg = mo.seekWidgetByName(this.Panel_huamian, "Image_tableBg" + i);
            Image_tableBg.setTag(i);
            Image_tableBg.addTouchEventListener(function (sender, type) {
                if (mo.gameHelper.getIsTouchEnd(type)) {
                    mo.gameUserInfo.setGameKwxBgIndex("" + (sender.getTag() + 1));
                }
            }, this);
        }

        this.mCardBgUIs = [];
        for (var i = 0; i < 3; i++){
            var Panel_cardBg = mo.seekWidgetByName(this.ccsNode, "Panel_cardBg" + i);
            var Image_text = mo.seekWidgetByName(Panel_cardBg, "Image_text");
            this.mCardBgUIs.push(Image_text);

            Panel_cardBg.setTag(i);
            Panel_cardBg.addTouchEventListener(function (sender, type) {
                if (mo.gameHelper.getIsTouchEnd(type)) {
                    mo.gameUserInfo.setGameKwxCardIndex("" + (sender.getTag() + 1));
                }
            }, this);
        }

        //切换tab
        this.Image_tabs = [];
        for (var i = 0; i < 2; i++){
            var Image_tab = mo.seekWidgetByName(this.ccsNode, "Image_tab" + i);
            this.Image_tabs.push(Image_tab);

            var Panel_tabTouch = mo.seekWidgetByName(this.ccsNode, "Panel_tabTouch" + i);
            Panel_tabTouch.setTag(i);
            Panel_tabTouch.addTouchEventListener(function (sender, type) {
                if (mo.gameHelper.getIsTouchEnd(type)) {
                    this.intoTabIndex(sender.getTag());
                }
            }, this);
        }
        this.intoTabIndex(0);

        //隐藏
        this.setVisible(false);

        //调整ui
        if (this.mGameName != "kwx"){
            this.Panel_huamian.setVisible(false);
            mo.seekWidgetByName(this.ccsNode, "Image_tabBg").setVisible(false);
            mo.seekWidgetByName(this.Panel_setting, "Panel_yuyan").setVisible(false);
            mo.seekWidgetByName(this.Panel_setting, "Panel_effect").setPositionY(60);
            mo.seekWidgetByName(this.Panel_setting, "Panel_music").setPositionY(-40);
            mo.seekWidgetByName(this.Panel_setting, "Button_ok").setPositionY(-140);

        }else{
            mo.seekWidgetByName(this.ccsNode, "Image_title").setVisible(false);
        }
    },

    onEnter: function () {
        this._super();

        //音乐
        this.updateMusicAndEffets(mo.gameUserInfo.mMusicVolume, mo.gameUserInfo.mEffectsVolume);
        mo.eventHelper.add("updateMusicAndEffects", this, function () {
            this.updateMusicAndEffets(mo.gameUserInfo.mMusicVolume, mo.gameUserInfo.mEffectsVolume);
        });

        if (this.mGameName == "kwx"){
            //方言
            this.updateLocalism();
            mo.eventHelper.add("updateLocalism", this, function(){
                this.updateLocalism();
            });

            //牌背和桌面背景
            this.updateSelect();
            mo.eventHelper.add("changeKwxCardBg", this, function(){
                this.updateSelect();
            });
            mo.eventHelper.add("changeKwxTableBg", this, function(){
                this.updateSelect();
            });
        }
    },

    onExit: function () {
        mo.eventHelper.remove(this);

        this._super();
    },

    updateMusicAndEffets: function (musicVolume, effectsVolume) {

        mo.seekWidgetByName(this.ccsNode, "Slider_music").setPercent(musicVolume);

        mo.seekWidgetByName(this.ccsNode, "Slider_effect").setPercent(effectsVolume);
    },

    sliderEvent: function (sender, type) {
        switch (type) {
            case ccui.Slider.EVENT_PERCENT_CHANGED:
                if (this.Slider_effect == sender) {
                    var slider = sender;
                    var percent = slider.getPercent();
                    percent = parseInt(percent);
                    mo.gameUserInfo.mEffectsVolume = percent;
                    mo.soundHelper.setEffectsVolume(percent);
                } else {
                    var slider = sender;
                    var percent = slider.getPercent();
                    percent = parseInt(percent);
                    mo.gameUserInfo.mMusicVolume = percent;
                    mo.soundHelper.setMusicVolume(percent);
                }
                break;
            default:
                break;
        }
    },

    setDialog: function (isVisible) {
        this.setVisible(isVisible);
        if (!isVisible) {
            mo.gameUserInfo.setMusicAndEffects(parseInt(this.Slider_music.getPercent()), parseInt(this.Slider_effect.getPercent()));
        }
    },

    //设置本地方言
    updateLocalism : function(){
        var index = mo.gameUserInfo.mLocalism;
        for (var i = 0; i < 6; i++){
            var item = this.mPanel_yuyans[i];
            var check = mo.seekWidgetByName(item, "CheckBox_select");
            check.setSelected(i == index);
        }
    },

    intoTabIndex : function(index){
        this.Image_tabs[0].setVisible(index == 0);
        this.Image_tabs[1].setVisible(index == 1);

        //ui
        if (index == 0){
            this.Panel_setting.setVisible(true);
            this.Panel_huamian.setVisible(false);
        }else if (index == 1){
            this.Panel_setting.setVisible(false);
            this.Panel_huamian.setVisible(true);
        }
    },

    updateSelect : function(){

        //牌背
        var cardBgIndex = mo.gameUserInfo.mGameKwxCardIndex;
        var selectIndex = parseInt(cardBgIndex) - 1;
        for (var i = 0; i < 3; i++){
            mo.gameHelper.loadTextureImage(this.mCardBgUIs[i], selectIndex == i ? "res/ccs/table/box/check_select.png" : "res/ccs/table/box/check_bg.png");
        }

        //桌面背景
        var tableBgIndex = mo.gameUserInfo.mGameKwxBgIndex;
        selectIndex = parseInt(tableBgIndex) - 1;
        this.Image_tableBgSelect.setPosition(mo.seekWidgetByName(this.ccsNode, "Image_tableBg" + selectIndex).getPosition());
    },

    //发送解散房间
    sendJiesanTable : function(){
        if (this.mGameName == "kwx"){
            if (mo.kwxTable.mPlayerType == 0){
                // mo.kwxTable.mKwxReplayLayer.onPlayEnd();
            }else {
                if ((mo.kwxTable.mTableConfig.maxpan == mo.kwxTableStatus.mTableStatus.LeftPlayTimes) &&
                    mo.kwxTableStatus.mTableStatus.Step == 0){
                    mo.gameMsgManager.sendForceExitTableRequestkwx();
                }else{
                    mo.kwxTable.mKwxTableUILayer.mKwxApplyTableLayer.sendCancelTableAgree();
                }
            }
        }else if (this.mGameName == "nn"){
            if (mo.nnTable.mPlayerType == 0){
                // mo.nnTable.mNnReplayLayer.onPlayEnd();
            }else{
                if ((mo.nnTableStatus.mTableStatus.LeftPlayTimes == mo.nnTable.mGameConfig.maxpan) &&
                    mo.nnTableStatus.mTableStatus.Step == 0){
                    mo.gameMsgManager.sendForceExitTableRequestnn();
                }else{
                    mo.nnTable.mKwxApplyTableLayer.sendCancelTableAgree();
                }
            }
        }else if (this.mGameName == "gsbdx"){
            if (mo.gsbdxTable.mPlayerType == 0){
                // mo.gsbdxTable.mNnReplayLayer.onPlayEnd();
            }else{
                if ((mo.gsbdxTableStatus.mTableStatus.LeftPlayTimes == mo.gsbdxTable.mGameConfig.maxpan) &&
                    mo.gsbdxTableStatus.mTableStatus.Step == 0){
                    mo.gameMsgManager.sendForceExitTableRequestgsbdx();
                }else{
                    mo.gsbdxTable.mKwxApplyTableLayer.sendCancelTableAgree();
                }
            }
        }else if (this.mGameName == "psz"){
            if (mo.pszTable.mPlayerType == 0){
                // mo.pszTable.mNnReplayLayer.onPlayEnd();
            }else{
                if ((mo.pszTableStatus.mTableStatus.LeftPlayTimes == mo.pszTable.mGameConfig.maxpan) &&
                    mo.pszTableStatus.mTableStatus.Step == 0){
                    mo.gameMsgManager.sendForceExitTableRequestpsz();
                }else{
                    mo.pszTable.mKwxApplyTableLayer.sendCancelTableAgree();
                }
            }
        }else if(this.mGameName == "dzpk"){
            if (mo.dzpkTable.mPlayerType == 0){
            }else{
                if ((mo.dzpkTableStatus.mTableStatus.LeftPlayTimes == mo.dzpkTable.mGameConfig.maxpan) &&
                    mo.dzpkTableStatus.mTableStatus.Step == 0){
                    mo.gameMsgManager.sendForceExitTableRequestdzpk();
                }else{
                    mo.dzpkTable.mKwxApplyTableLayer.sendCancelTableAgree();
                }
            }
        }else if(this.mGameName == "dtj"){
            if (mo.dtjTable.mPlayerType == 0){

            }else{
                if ((mo.dtjTableStatus.mTableStatus.LeftPlayTimes == mo.dtjTable.mGameConfig.maxpan) &&
                    mo.dtjTableStatus.mTableStatus.Step == 0){
                    mo.gameMsgManager.sendForceExitTableRequestdtj()
                }else{
                    mo.dtjTable.mKwxApplyTableLayer.sendCancelTableAgree();
                }
            }
        }else if(this.mGameName == "ddz"){
            if (mo.ddzTable.mPlayerType == 0){

            }else{
                if ((mo.ddzTableStatus.mTableStatus.LeftPlayTimes == mo.ddzTable.mGameConfig.maxpan) &&
                    mo.ddzTableStatus.mTableStatus.Step == 0){
                    mo.gameMsgManager.sendForceExitTableRequestddz()
                }else{
                    mo.ddzTable.mKwxApplyTableLayer.sendCancelTableAgree();
                }
            }
        }else if(this.mGameName == "pdk"){
            if (mo.pdkTable.mPlayerType == 0){

            }else{
                if ((mo.pdkTableStatus.mTableStatus.LeftPlayTimes == mo.pdkTable.mGameConfig.maxpan) &&
                    mo.pdkTableStatus.mTableStatus.Step == 0){
                    mo.gameMsgManager.sendForceExitTableRequestpdk()
                }else{
                    mo.pdkTable.mKwxApplyTableLayer.sendCancelTableAgree();
                }
            }
        }
    }
});